duanfu3634 2013-04-27 10:31
浏览 106
已采纳

YUI JavaScript - 如何在select onchange事件后添加输入文本框?

How can I insert an additional input textbox after an onchange event in a selectbox with the YUI library?

It is required to use YUI and the form is created with the PEAR Quickforms lib.

What I have yet is this:

$form->addElement('select', 'names', "Choose Name", $options, array('style'=>'width:300px', 'onchange' => 'name_changed(this.value);'));

$js = 
<<<EOS
<script type="text/javascript">
    function name_changed(name) {
        alert('name is changed');
    }
</script>
EOS;

$form->addElement('static', 'jsembed', '', utf8_encode($js));

The alert pops up if I change the option in the selectbox, this works for testing.

Now I need to check the chosen option and if this equals 'NEW', there should appear a text input field to enter a new name under the select box.

My problem is, that I don't know to do it right that the entered name is also passed to the submitted quickform data.

So I've created the element normally with $form->addElement('input',...) and copied the html source of this. On the option change event I've tried to insert the raw html code at the right position with this:

var htmlContent = '<div id="fitem_id_dbname" class="fitem fitem_ftext"><div class="fitemtitle"><label for="id_dbname">Create new DB </label></div><div class="felement ftext" id="yui_275"><input size="60" name="dbname" type="text" value="" id="id_dbname"></div></div>';
document.getElementsByClassName('fcontainer clearfix').innerHTML = htmlContent;

But this doesn't work, there appears no input field in the browser.

Could anybody show me how to do it right? Lots of thanks!

  • 写回答

1条回答 默认 最新

  • doujingxi3356 2013-04-29 11:51
    关注

    I solved it for me now with the...

    'style'=>'display:none'
    

    ...property in the quickform textinput element and use...

    document.getElementById('id of the input element').style.display = 'block';
    

    ... which is called on the onchange event.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系