gpt4 book ai didi

javascript - 想要从弹出窗口中的单选按钮获得结果

转载 作者:行者123 更新时间:2023-11-30 12:23:22 24 4
gpt4 key购买 nike

我的代码:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>
<h2>Hello</h2>
<button type="button" id="example" class="btn btn-danger" data-container="body" data-html="true" data-toggle="popover" data-placement="right" data-content="&lt;form id='myform' class='form-horizontal'&gt;
&lt;input type='radio' id='rb1' name='keys' value='Adult' bind-value='key'&gt;
Adult &lt;br&gt;
&lt;input type='radio' id='rb2' name='keys' value='Child' bind-value='key'&gt;
Child &lt;br&gt;
&lt;input type='radio' id='rb3' name='keys' value='Concession' bind-value='key'&gt;
Concession &lt;/form&gt;" data-original-title="Select seat type">Hello
</button>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#example').popover();
$('#myform input').on('change', function() {
alert($('input[name=keys]:checked').val());
});
});
</script>
</body>

</html>

我想在警报中显示所选单选按钮(放置在弹出窗口中)的值。此代码以某种方式根本不会发出警报。弹出框显示完美,单选按钮也显示正确。只有在弹出窗口中选择单选按钮时不会显示警报。请帮忙!

最佳答案

问题在于何时初始化监听器。在显示弹出窗口之前,表单不存在。所以你需要在之后初始化事件监听器。

这是片段:

$(document).ready(function() {
$('#example').popover();

$('#example').on('shown.bs.popover', function() {
$('#myform input').on('click', function() {
alert($('input[name=keys]:checked').val());
});
});

});
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>

<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
</head>

<body>
<h2>Hello</h2>
<button type="button" id="example" class="btn btn-danger" data-container="body" data-html="true" data-toggle="popover" data-placement="right" data-content="&lt;form id='myform' class='form-horizontal'&gt;
&lt;input type='radio' id='rb1' name='keys' value='Adult' bind-value='key'&gt;
Adult &lt;br&gt;
&lt;input type='radio' id='rb2' name='keys' value='Child' bind-value='key'&gt;
Child &lt;br&gt;
&lt;input type='radio' id='rb3' name='keys' value='Concession' bind-value='key'&gt;
Concession &lt;/form&gt;" data-original-title="Select seat type">Hello
</button>
</body>

</html>

关于javascript - 想要从弹出窗口中的单选按钮获得结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30297060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com