Javascript 我仍然非常确信最近的(‘形式’)是正确的。console.log(form)语句起作用,表明我选择了正确的表单。可能您只需要将click事件更改为您要侦听的事件,以及$('form')的警报。serialize()您还需要添加一个比输入选择器更好的选择器:
Javascript 我仍然非常确信最近的(‘形式’)是正确的。console.log(form)语句起作用,表明我选择了正确的表单。可能您只需要将click事件更改为您要侦听的事件,以及$('form')的警报。serialize()您还需要添加一个比输入选择器更好的选择器:,javascript,jquery,html,dynamic,Javascript,Jquery,Html,Dynamic,我仍然非常确信最近的(‘形式’)是正确的。console.log(form)语句起作用,表明我选择了正确的表单。可能您只需要将click事件更改为您要侦听的事件,以及$('form')的警报。serialize()您还需要添加一个比输入选择器更好的选择器:),因此您只需侦听页面中需要的那些元素。确定,因此,如果您放置$(“#段落列表”).empty();语句中,它不起作用。填充paragraphList的代码会重置该列表,然后再填充它,.empty()似乎会中断jQuery侦听器。当然,如果将其
我仍然非常确信最近的(‘形式’)是正确的。console.log(form)语句起作用,表明我选择了正确的表单。可能您只需要将click事件更改为您要侦听的事件,以及$('form')的警报。serialize()您还需要添加一个比输入选择器更好的选择器:),因此您只需侦听页面中需要的那些元素。确定,因此,如果您放置$(“#段落列表”).empty();语句中,它不起作用。填充paragraphList的代码会重置该列表,然后再填充它,.empty()似乎会中断jQuery侦听器。当然,如果将其设置为空,它实际上会将其从dom中删除,并将所有侦听器都删除到它。为什么要将其设置为空?里面有东西吗?
$('#paragraphsList').on('change', 'input', function (e) {
e.preventDefault();
var form = this.closest("form");
console.log(form);
console.log(form.serialize());
<ul id="paragraphsList">
<--dynamicaly generated content begins here-->
<li>
<form class="paragraphForm" name="80" id="80">
<input id="81" name="activity" value="a" class="paragraphs">
<input id="82" name="task" value="b" class="paragraphs">
<input id="83" name="pStyle" value="c" class="paragraphs">
<input id="84" name="pContent" value="d" class="paragraphs,style2">
</form>
</li>
<li>
<form class="paragraphForm" name="85" id="85">
<input id="86" name="activity" value="a" class="paragraphs">
<input id="87" name="task" value="b" class="paragraphs">
<input id="88" name="pStyle" value="c" class="paragraphs">
<input id="89" name="pContent" value="d" class="paragraphs,style2">
</form>
</li>
.....
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<p onclick="load()">Load Forms</p>
<ul id="paragraphsList">
</ul>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function load() {
$('#paragraphsList').empty();
$('#paragraphsList').append(newForm("A", "step1", "task1", "myStyle1", "myContent1"));
$('#paragraphsList').append(newForm("b", "step2", "task2", "myStyle2", "myContent2"));
$('#paragraphsList').append(newForm("c", "step3", "task3", "myStyle3", "myContent3"));
$('#paragraphsList').append(newForm("d", "step4", "task4", "myStyle4", "myContent4"));
}
function newForm(id, activity, task, pStyle, pContent) {
var li = $('<li></li>');
var form = $("<form></form>");
form.attr("class", "paragraphForm");
form.attr("name", id);
form.attr("id", id);
form.append(newField(id, "activity", activity, "paragraphs", "data" ));
form.append(newField(id, "task", task, "paragraphs", "data" ));
form.append(newField(id, "pStyle", pStyle, "paragraphs", "data" ));
form.append(newField(id, "pContent", pContent, "paragraphs," + pStyle, "data"));
form.change(function () {
console.log(this);
console.log(this.serialize());
});
li.append(form);
return li;
}
function newField(id, name, value, style) {
var input = $("<input>");
input.attr("id", id + "." + name);
input.attr("name", name);
input.attr("value", value);
input.attr("class", style);
return input;
}
</script>
</html>
$( ".paragraphForm" ).on( "change", function( event ) {
event.preventDefault();
console.log( $( this ).serialize() );
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>serialize demo</title>
<style>
body, select {
font-size: 12px;
}
form {
margin: 5px;
}
p {
color: red;
margin: 5px;
font-size: 14px;
}
b {
color: blue;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form>
<select name="single">
<option>Single</option>
<option>Single2</option>
</select>
<br>
<select name="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select>
<br>
<input type="checkbox" name="check" value="check1" id="ch1">
<label for="ch1">check1</label>
<input type="checkbox" name="check" value="check2" checked="checked" id="ch2">
<label for="ch2">check2</label>
<br>
<input type="radio" name="radio" value="radio1" checked="checked" id="r1">
<label for="r1">radio1</label>
<input type="radio" name="radio" value="radio2" id="r2">
<label for="r2">radio2</label>
</form>
<p><tt id="results"></tt></p>
<script>
function showValues() {
var str = $( "form" ).serialize();
$( "#results" ).text( str );
}
$( "input[type='checkbox'], input[type='radio']" ).on( "click", showValues );
$( "select" ).on( "change", showValues );
showValues();
</script>
</body>
</html>
$('#paragraphsList > form > input')
<ul id="paragraphsList">
</ul>
$(document).ready(function () {
var form = $('<li><form class="paragraphForm" id="form1" name="80" id="80"><input id="81" name="activity" value="a" class="paragraphs"><input id="82" name="task" value="b" class="paragraphs"><input id="83" name="pStyle" value="c" class="paragraphs"><input id="84" name="pContent" value="d" class="paragraphs,style2"></form></li>');
$('#paragraphsList').append(form);
$("input").click(function () {
alert('you clicked me');
console.log($('form').serialize());
});
});
console.log($(form).serialize());