[英]Passing variable into a modal from a script
如果要使用基于用户操作在脚本内部更新的变量(例如,@ myVar)来更新模态,该如何进行呢? 因为脚本内部的变量与脚本外部的变量不同,所以我不确定脚本内部的某些内容如何在不知道id的情况下更新脚本外部的内容。
我们是否需要模态中的id标签,以允许从脚本中进行某种类型的更新? 如果是这样,除了
,我们可以使用它来允许我们附加一个ID -如果是这样的话?
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
@{var myVar = "______";}
@{var myVar2 = "______";}
...
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModal-label">A list of your choices</h4>
</div>
<div class="modal-body">
<p>My first choice would be @myVar and my second choice would be @myVar2.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Edit</button>
<button type="button" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</div>
<script id="myScript" data-myvar="@myVar">
$('[name="optradio1"]').on('change', function () {
$('#accordion-second').slideToggle();
$('#accordion-first').slideToggle();
var value = $(this).val();
switch (value) {
case "1":
$('#myScript').data('myVar', "Option 1");
alert($('#myScript').data('myVar'));
break;
case "2":
$('#myScript').data('myVar', "Option 2");
alert($('#myScript').data('myVar'));
break;
default:
alert("neither options were chosen");
}
});
</script>
<script id="myScript2" data-myvar="@myVar2">
$('[name="optradio2"]').on('change', function () {
$('#accordion-second').slideToggle();
$('#accordion-third').slideToggle();
var value = $(this).val();
switch (value) {
case "3":
$('#myScript2').data('myVar2', "Option 1");
alert($('#myScript2').data('myVar2'));
break;
case "4":
$('#myScript2').data('myVar2', "Option 2");
alert($('#myScript2').data('myVar2'));
break;
default:
alert("neither options were chosen");
}
$('#myModal').modal('toggle');
});
</script>
...
`
</body>
</html>
是的,您的思考方向正确。 您需要以某种方式在模态中处理元素。 您可以使用ID,类,某些特殊属性等。让我们以ID为例。 我看到您已经在模态本身上有了ID-“ myModal”假设,在某些用户操作中,您希望将变量的值分配给模态上的文本框。 你会写这样的东西
<div id="myModal">
...
<input type="text" id="txtUserName"></input>
...
</div>
<script>
...
$('#txtUserName',$('#myModal')).val('Jim Smith');
...
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.