简体   繁体   English

提交值<select>表单中div内的字段

[英]submitting value of <select> field inside div in form

i am trying to submit values of form.我正在尝试提交表单的值。
but all values other than a <select> field is not submitted because i think its inside div element as shown in code below但是除<select>字段之外的所有值都没有提交,因为我认为它的内部 div 元素如下面的代码所示

<form action="next.php" method="post">
     <input>....
     <input>....
     <input>....
     <select name="asdf"> <!--This field submitted successfully -->
         <option value="wtevr"> </option>
         <option value="wtevr2"> </option>..........
     </select>
     <div>
         <select name="asdf2"> <!--problem is here submitting this field -->
             <option value="wtevr11"> </option>
             <option value="wtevr22"> </option>..........
         </select>
     </div>
</form>

I used div because this is part of my ajax code through which i update the values in select field.我使用div是因为这是我的 ajax 代码的一部分,我通过它更新选择字段中的值。

Is there any alternative solution to submit this select field form?是否有其他解决方案来提交此select字段表单?

MY approach:我的方法:

I think that there is a solution if i use a hidden input field in this form whose value get equal to value of select field before submitted the form.我认为如果我在此表单中使用hidden input字段,其值在提交表单之前等于选择字段的值,则有一个解决方案。

<input type="hidden" value="<!--comes from JavaScript code -->">

I am new to JavaScript so any help is appreciated..我是 JavaScript 的新手,所以任何帮助表示赞赏..

You could use:你可以使用:

<select name="asdf2" id="asdf2" onchange="setField();">
            <option value="this">this</option>
            <option value="that">that</option>
        </select>
        <input type="hidden" id="hiddenField" value="nothingYet">
<script type="text/JavaScript" language="JavaScript">
function setField() {
            toWhat = document.getElementById("asdf2").value
            document.getElementById("hiddenField").value = toWhat;
        }
</script>

In any case.任何状况之下。 If all you're doing is submitting the form, there really is no reason for that value to be ignored.如果您所做的只是提交表单,那么确实没有理由忽略该值。 How are you retrieving the values?你如何检索值?

Use a 'name' tag in your inputs, then they'll be submitted.在您的输入中使用“名称”标签,然后它们将被提交。 like this:像这样:

<form action="next.php" method="post">
<input name="foo" value="var"/>
</form>

The <select> tag has a property called [form=""]. <select>标签有一个名为 [form=""] 的属性。 With that you can assign it to a form element.有了它,您可以将其分配给表单元素。 As the form gets submitted, the select element is included.当表单被提交时,选择元素被包括在内。

<form name="myform">
    <input type="text" name =...>
    <select name="selectelement" form="myform">
    <input type="submit">
</form>

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

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