2016-09-16 35 views
1

JQuery的新功能和我有以下功能。我试图修改它,所以它做了2件事:当我点击其他单选按钮时,不会触发该功能(我认为'#UpdateType input [type =“radio”]'会这样做),并且也会在加载时运行该函数如点击。 #UpdateType是我正在使用它的单选按钮的ID。有没有人有什么建议?JQuery函数 - 在点击和页面加载时运行特定的单选按钮

$(document).ready(updateTypeFunc); 
$(document).ready(function() { 
    $('#UpdateTypeRB input[type="radio"]').click(updateTypeFunc); 
}); 
function updateTypeFunc() 
{ 
    if ($(this).attr('id') == 'UpdateType' && $(this).val() == 'Current') { 
     $('.current-employee').show(); 
    } 
    else { 
     $('.current-employee').hide(); 
    } 
} 

截至目前,当我点击任何单选按钮(无论ID如何)时都会触发。

编辑:HTML

<div class="col-md-10" id="UpdateTypeRB"> 
    <label class="radio-inline"> 
     <input class="btn btn-primary" data-val="true" data-val-required="Please select whether the employee is new or current." id="UpdateType" name="UpdateType" type="radio" value="New"> New 
    </label> 
    <br> 
    <label class="radio-inline"> 
     <input checked="checked" class="btn btn-secondary" id="UpdateType" name="UpdateType" type="radio" value="Current"> Current 
    </label> 
    <br> 
    <span class="field-validation-valid text-danger" data-valmsg-for="UpdateType" data-valmsg-replace="true"></span> 
</div> 

剃刀:

<div class="form-group"> 
    @Html.LabelFor(m => m.UpdateType, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     <label class="radio-inline"> 
      @Html.RadioButtonFor(m => m.UpdateType, "New", new { @class = "btn btn-primary" }) New 
     </label> 
     <br /> 
     <label class="radio-inline"> 
      @Html.RadioButtonFor(m => m.UpdateType, "Current", new { @class = "btn btn-secondary" }) Current 
     </label> 
     <br /> 
     @Html.ValidationMessageFor(m => m.UpdateType, "", new { @class = "text-danger" }) 
    </div> 
</div> 

编辑2:更新JQuery的感谢@KristianBarrett。除了我试图实现的$(document).ready以外的其他作品。

我总体努力实现的是隐藏/显示基于此单选按钮打勾的页面上的某些必需元素。它最初加载没有检查,所以用户被迫选择一个。然后,我使用Foolproof来根据这些复选框('Current'tick)执行RequiredIf,并且想要在用JQuery勾选'Current'之后向用户显示它们。问题是验证在提交后抛出一个ErrorMessage时,当前复选框被打勾,但是这些字段被隐藏,直到我再次手动点击它。这些字段默认为隐藏。

+0

你的HTML看起来像什么? '$('#UpdateType input [type =“radio”]')'表示运行这个处理器,处理ID为“UpdateType”的容器中的任何无线电。 – tymeJV

+0

被编辑。那么这是否意味着我的Razor语法会因为#UpdateType的两个ID而搞砸了? – justiceorjustus

+1

好吧 - 你不能有两个相同的ID - ** ID必须是唯一的** – tymeJV

回答

1

你需要作出两种不同的选择:

<div class="form-group"> 
@Html.LabelFor(m => m.UpdateType, new { @class = "col-md-2 control-label" }) 
<div class="col-md-10"> 
    <label class="radio-inline"> 
     @Html.RadioButtonFor(m => m.UpdateType, "New", new { @class = "btn btn-primary new" }) New 
    </label> 
    <br /> 
    <label class="radio-inline"> 
     @Html.RadioButtonFor(m => m.UpdateType, "Current", new { @class = "btn btn-secondary current" }) Current 
    </label> 
    <br /> 
    @Html.ValidationMessageFor(m => m.UpdateType, "", new { @class = "text-danger" }) 
</div> 

那么你可以申请@checked = true你的单选按钮,你想拥有点击页面加载。像这样

@Html.ValidationMessageFor(m => m.UpdateType, "", new { @class = "text-danger", @checked = true }) 

当你绑定你的价值观,以两个不同的值你的模型,那么你的服务器将解析选择之一,应该正确设置它们一旦返回你的看法。

之后,你需要应用stweb给你的答案,但你需要寻找检查一个。

$(document).ready(function() { 
    $('input[type="radio"]').click(function() { 
     clickFunc($(this)); 
    }); 
    clickFunc($('input[type=radio]:checked')); 
}); 

function clickFunc($element){ 
    var currentEmployee = $('.current-employee'); 
    if ($element.attr('class').indexOf('current') !== -1) { 
     currentEmployee.show(); 
    } 
    else { 
     currentEmployee.hide(); 
    } 
} 

你需要重新申请的JavaScript每次页面运行:

所以像你应该做的事情。所以你迭代你的复选框并找到选中的并点击它。

$('input[type=radio]:checked') 

这会给你选中的单选按钮。然后,你可以将它传递给clickFunc上$(document).ready()

编辑:

你正在做的是硬编码针对某些特定的ID另一件事。你应该更普遍地思考。就像选择所有单选按钮,然后找到选中的单选按钮。当您更改模型的ID时,这会使您的代码更容易出错。你可以制作代码越通用,就越不需要维护。

+0

试图实现这个时遇到了一些问题。首先,它允许我一次检查两个单选按钮。其次,即使该单选按钮被勾选,jquery也不起作用。这可能是因为UpdateType的ID现在是CurrentType。即使如此,当模型更改后指向#CurrentType时,它不会显示或隐藏。编辑:另外,当它在使用UpdateType两次时发生验证错误(至少明显)后进入视图时,它会打勾正确的。不过,我相信你认为这是一种不正确的做法,并希望以正确的方式做到这一点。 – justiceorjustus

+0

对不起,这是漫长的一天。当然,你将它们绑定到同一个模型上是正确的,这使得你只能选择一个模型。我会更新它给我一分钟。 –

+0

在现实中,我现在唯一的问题是在此时加载页面加载我的jquery函数(编辑我当前的JS在我的原始文章中适用于点击)。模型和视图看起来很好。 – justiceorjustus

1

要触发负荷试试这个:

$(document).ready(function() { 
    var $radioButton = $('#UpdateType input[type="radio"]'); 
    $radioButton.click(function() { 
     clickFunc($(this)); 
    }); 
    clickFunc($radioButton); 
}); 

function clickFunc($element){ 
    var currentEmployee = $('.current-employee'); 
    if ($element.attr('id') == 'UpdateType' && $element.val() == 'Current') { 
     currentEmployee.show(); 
    } 
    else { 
     currentEmployee.hide(); 
    } 
} 
+0

对不起,但这真的不是一个好主意。相反,您应该在新函数(clickFunc)中重构click函数的内部。然后调用clickFunc,而不是触发点击按钮。 –

+0

从技术上讲,这是有效的,但并不像我想象的那样。现在我没有选择单选按钮,所以我可以在MVC中验证他们必须单击一个。这只是检查蝙蝠上方的单选按钮,它触发了上面的功能,对吗?我的问题是,当MVC中的验证返回并重新加载表单时,当'Current'单选按钮被选中并保留在以前的模型中时,它不会触发该函数。 – justiceorjustus

+0

@KristianBarrett,谢谢,同意你的评论。更新。 – stweb

相关问题