I have a form in ASP.NET MVC which contains a DropDownList
where I need to serialize the text value of the currently selected option in the list, not the value of that option. Then, I need to serialize a TextArea
and send those both in the same query string. I have tried to select my DropDownList
text using this JQuery statement:
var _dropDown = $('#ConfigList option:selected').text().serialize();
But I get the error:
Uncaught TypeError: $(...).text(...).serialize is not a function
My Question
How do I get the text value of my dropdownlist and my textarea, and serialize them together? They are both in the same input field which I am passing in to my JQuery function that serializes them.
答案 0 :(得分:1)
// Bind a handler to update the hidden input when the select changes
$("select").on("change", function() {
$("#my_hidden").val(this[this.selectedIndex].text);
}).change();
// serialize it
$("button").on("click", function() {
var res = $("#my_textarea, #my_hidden").serializeArray();
$("pre").text(JSON.stringify(res, null, 2));
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="my_hidden" name=my_hidden type="hidden">
<select>
<option value="FOO_VALUE">foo
<option value="BAR_VALUE">bar
</select>
<br><textarea id="my_textarea" name=my_textarea>some content</textarea>
<br><button>serialize</button>
<pre></pre>
&#13;
答案 1 :(得分:-1)
要序列化所有表单值,您必须使用表单的 ID ,它会自动将所有字段值序列化为序列化表单。此外,如果您使用 .text(),它将删除所有html标记(如果有),因此请确保在添加 .text()之前进行serilize。
现在对于下拉列表,如果你真的不需要任何地方的值标签,你为什么要这样做。删除它然后它将自动获取在下拉列表中可见的文本值。
var serialized = $("#form_name").serialize();