简体   繁体   English

单击下拉列表,创建一个表单

[英]Drop-down clicked, a form created

enter image description here This error message keeps opening on console. 在此处输入图像描述此错误消息在控制台上不断打开。 And I am using Ember.js 我正在使用Ember.js

I am trying to make a drop-down and whenever on option from a drop-down is clicked, a form should be made based on what an option is chosen. 我试图进行下拉,每当单击下拉菜单中的on选项时,都应根据选择的选项来制作表格。 For example, there are 3 options on dropdown: name, text, drop-down. 例如,下拉菜单上有3个选项:名称,文本,下拉菜单。 When a user click a text, a text form should be created below. 用户单击文本时,应在下面创建文本表单。 I already made an dropdown and tried to implement by writing document.write(" < /h1>"), but it keeps saying uncaught syntax error. 我已经做了一个下拉列表,并试图通过编写document.write(“ </ h1>”)来实现,但是它一直在说未捕获的语法错误。 Can someone help me please? 有人能帮助我吗?

<script type="text/javascript">
    function clicking(option) {
        if (option === "name")
            //document.write("<h1>Hello<h1>");
    }
</script>

<h1>Data Form Test</h1>
<div id="dropdown">
    <form>
        <select id="selectBox" onchange="clicking(this)">
            <option value="" disabled="disabled" selected="selected" style="display:none">Please select a option</option>
            <option value="name">Name</option>
            <option value="title">Title</option>
            <option value="text">Text</option>
            <option value="check-box">Check-box</option>
            <option value="drop-down">Drop-down</option>
            <option value="calendar">Calendar</option>
        </select>
    </form>
</div>

<div id="div1"></div>
<script type="text/javascript">
    function clicking(option) {
        if (option == "name"){
            //document.write("<h1>Hello<h1>");
        }
    }
</script>

and on html 和在HTML上

onchange="clicking(this.value)"

You can't access your selected value via option , you need to use property value instead. 您无法通过option访问选定的值,而需要使用属性value In my example I changed option with event . 在我的示例中,我更改了event option

To write some HTML/text into div/selector use .innerHTML method instead of document.write . 要将一些HTML /文本写入div / selector中,请使用.innerHTML方法而不是document.write

See working example. 请参见工作示例。

  function clicking(event) { if (event.value === "name") document.getElementById('div1').innerHTML = "<h1>Hello<h1>"; } 
 <h1>Data Form Test</h1> <div id="dropdown"> <form> <select id="selectBox" onchange="clicking(this)"> <option value="" disabled="disabled" selected="selected" style="display:none">Please select a option</option> <option value="name">Name</option> <option value="title">Title</option> <option value="text">Text</option> <option value="check-box">Check-box</option> <option value="drop-down">Drop-down</option> <option value="calendar">Calendar</option> </select> </form> </div> <div id="div1"></div> 

If you would like to use your form to perform for example an ajax request. 如果您想使用form执行例如ajax请求。 Here is another example I created for you. 这是我为您创建的另一个示例。

I used addEventListener to show to different approach of handling your clicking function. 我使用addEventListener展示了处理clicking功能的不同方法。

Read my comments. 阅读我的评论。

 // Our selectors var form = document.getElementById('example-form'); var select = document.getElementById('selectBox'); var result = document.getElementById('result'); // Let's add an event listener to our form, we will listen whenever we submit the form form.addEventListener('submit', function(e) { var elements = this.querySelectorAll('input, select'); var formData = {}; for(i = 0; i < elements.length; i++) { var element = elements[i]; Object.assign(formData, { [element.name] : element.value }) } console.log(formData); // Now you can perform some ajax call eg. // I've commented it out, but code works, you just need to replace url //$.ajax({ // url: 'http://example.com/action/url/', // type: 'post', // data: formData, // our form data // success: function(response) { // result.innerHTML = response; // } //}) // Prevent Page from autoreloading e.preventDefault(); return false; }); // Another approach of handling your clicking function is by passing eventListener to select select.addEventListener('change', function(e) { // Log //console.log(e.target.value); // We can use switch here for example // Code becomes more readable switch (e.target.value) { case 'name': case 'title': case 'text': result.innerHTML = '<h1>Hello ' + e.target.value + '</h1>'; default: break; } }); 
  <form id="example-form"> <select id="selectBox" name="selectBox"> <option value="" disabled="disabled" selected="selected" style="display:none">Please select a option</option> <option value="name">Name</option> <option value="title">Title</option> <option value="text">Text</option> <option value="check-box">Check-box</option> <option value="drop-down">Drop-down</option> <option value="calendar">Calendar</option> </select> <input name="example1" value="" placeholder="example input 1" /> <input name="example2" value="" placeholder="example input 2" /> <button type="submit">Submit me</button> </form> <div id="result"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM