Javascript 根据单选按钮更改表单操作
我试图根据选中的单选按钮更改html表单的操作。我正在尝试使用jquery来实现这一点。我有一些代码,无法找出它不起作用的原因。有人能帮我把这个代码用起来吗 html:Javascript 根据单选按钮更改表单操作,javascript,jquery,html,Javascript,Jquery,Html,我试图根据选中的单选按钮更改html表单的操作。我正在尝试使用jquery来实现这一点。我有一些代码,无法找出它不起作用的原因。有人能帮我把这个代码用起来吗 html: if($(“#雇主”).prop(“checked”,true)){将始终返回true,因为您正在将属性设置为checked。您希望测试if($(“#雇主”).prop(“checked”)==true){ $(文档).ready(函数(){ $(“输入[name='status']”)。更改(函数(){ 如果($(“#雇主”
if($(“#雇主”).prop(“checked”,true)){
将始终返回true,因为您正在将属性设置为checked
。您希望测试if($(“#雇主”).prop(“checked”)==true){
$(文档).ready(函数(){
$(“输入[name='status']”)。更改(函数(){
如果($(“#雇主”).prop(“选中”)==true){
$(“#login”).attr(“action”,“DB/employerfication.php”)
}
否则{
$(“#login”).attr(“action”,“DB/userVerfification.php”)
}
});
});
登录
雇主
工作人员
如果($(“#雇主”).prop(“选中”,true)){
将始终返回true,因为您正在将属性设置为已选中。您希望测试如果($(“#雇主”).prop(“选中”)==true){
$(文档).ready(函数(){
$(“输入[name='status']”)。更改(函数(){
如果($(“#雇主”).prop(“选中”)==true){
$(“#login”).attr(“action”,“DB/employerfication.php”)
}
否则{
$(“#login”).attr(“action”,“DB/userVerfification.php”)
}
});
});
登录
雇主
工作人员
代码中的注释解释错误
$(文档).ready(函数(){
$(“输入[name='status']”)。更改(函数(){
//如果($(“#雇主”).prop(“选中”,true)){代码中的注释解释错误
$(文档).ready(函数(){
$(“输入[name='status']”)。更改(函数(){
//如果($(“#雇主”).prop(“checked”,true)){使用$(“#雇主”).is(“:checked”)
请参阅下面的提琴以供参考
使用$(“#雇主”).is(“:checked”)
请参阅下面的提琴以供参考
以下是动态更改操作的方法:
视图:
以下是如何动态更改操作:
视图:
如果您使用JS提交表单,则可以完全跳过该部分。或者只使用document.getElementById('login')。action=“newaction”;如果您使用JS提交表单,则可以完全跳过该部分。或者只使用document.getElementById('login')。action=“新行动”;
<form name=loginForm id='login' action='' method='post'>
<legend>Login</legend>
<input type='text' name='email' maxlength="100" placeholder="Email"/>
<input type='password' name='password' maxlength="100" placeholder="password"/>
<div class="checkBoxGroup">
<label for="Employer">Employer</label>
<input type="radio" name="status" id='employer' value="employer">
</div>
<div class="checkBoxGroup">
<label for="Staff">Staff</label>
<input type="radio" name="status" id='staff' value="staff">
</div>
<input type='submit' name='Submit' value='Login' />
</form>
$(document).ready(function(){
$("input[name='status']").change(function(){
if($("#employer").prop("checked", true)){
$("#login").attr("action", "DB/employerVerification.php")
}
else{
$("#login").attr("action", "DB/userVerfification.php")
}
}
$(document).ready(function(){
$("input[name='status']").change(function(){
//if($("#employer").prop("checked", true)){ <--- wrong / this set the property !
if($("#employer").prop("checked")){ // <--- this check the property
$("#login").attr("action", "DB/employerVerification.php")
}
else{
$("#login").attr("action", "DB/userVerfification.php")
}
alert($("#login").attr("action"));
}); // missing parentheses
}); // missing parentheses
@model Testy20161006.Controllers.theModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index57</title>
<script src="~/Scripts/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function () {
$("#setAction").click(function () {
//credit to http://stackoverflow.com/questions/5451600/jquery-to-change-form-action
if ($('input[name=changeAction]:checked').val() == "true") {
$("form").attr('action', 'ChangedAction');
}
})
})
</script>
</head>
<body>
<div>
@using (Html.BeginForm("Index57", "Home", FormMethod.Post))
{
//make sure N in Name is capital so it overrites the name property
@Html.RadioButtonFor(model => model.ChangedAction, "true", new { Name = "changeAction" })
<span>ChangedAction</span>
@Html.RadioButtonFor(model => model.ChangedAction, "false", new { Name = "changeAction" })
<span>NoActionChanged</span>
<input type="button" id="setAction" value="set Action" />
<input type="submit" id="theBtn" value="submit" />
}
</div>
</body>
</html>
public class theModel
{
public bool ChangedAction { get; set; }
}
public class HomeController : Controller
{
[HttpPost]
public ActionResult ChangedAction(theModel theModel)
{
return View();
}
[HttpPost]
public ActionResult Index57(theModel theModel)
{
return View();
}