简体   繁体   English

jQuery:如何将表单元素上的事件监听器范围扩展到自己的表单?

[英]jQuery: How to scope event listeners on form elements to their own forms?

So I have multiple forms on a page. 所以我在页面上有多个表单。 Each form is identical except for the data they may contain. 每个表单都是相同的,除了它们可能包含的数据。 I am listening for changes to a specific elements that should modify other elements within its same form, but I am having trouble figuring out how to scope those actions within its own form. 我正在侦听应该修改其相同形式的其他元素的特定元素的更改,但是我无法确定如何在自己的形式内调整这些操作的范围。 Here's what I've got so far: 这是我到目前为止所得到的:

var ContactSponsor = new function(){

    this.init = function(){
        $('#SponsorStatus_ID').change(SponsorStatusChanged);
    };

    var SponsorStatusChanged = function(){

        var $form = $(this).closest('form');
        var selectedValue = $(this).find('option:selected').text();

        if (selectedValue == 'Executed Contract') {
            markFieldRequired('#NumVAPs', $form);
            markFieldRequired('#SponsorWebsiteLevel_ID', $form);
            autoFillFieldWithCurrentDate('#ContractDate', $form);
        } else {
            markFieldNotRequired('#NumVAPs', $form);
            markFieldNotRequired('#SponsorWebsiteLevel_ID', $form);
        }
    };

    var markFieldRequired = function(fieldID, $form){
        $(fieldID, $form).siblings('label').addClass('required');
    };

    var markFieldNotRequired = function(fieldID, $form){
        $(fieldID, $form).siblings('label').removeClass('required');
    };

    var autoFillFieldWithCurrentDate = function(fieldID, $form){
        if ($(fieldID, $form).val() == '' || $(fieldID, $form).val() == '0000-00-00') {
            $(fieldID, $form).val(getCurrentDate());
        }
    };
};

Also, I'm passing the form to each of the methods. 另外,我将表单传递给每个方法。 Not sure if there is a way I can do this without passing it in every time. 不确定我是否有办法在不通过每次传递的情况下执行此操作。

It looks like you're using duplicate IDs. 看起来你正在使用重复的ID。 That is probably going to cause problems. 这可能会导致问题。 Rather than having each FORM include an input with id=NumVAPs , remove the ID and then target the field by name instead eg $form.find('[name=NumVAPs]') . 不是让每个FORM包含id=NumVAPs的输入, id=NumVAPs删除ID,然后按名称定位字段,例如$form.find('[name=NumVAPs]')

And hey, while we're at it, you might want to combine your markFieldRequired and markFieldNotRequired functions into a single setFieldRequired that accepts a boolean indicating whether the field should be required or not. 嘿,当我们在它时,您可能希望将markFieldRequiredmarkFieldNotRequired函数组合到一个setFieldRequired ,该setFieldRequired接受一个布尔值,指示是否需要该字段。

Also, if marking a field as required is purely cosmetic, you may want to consider handling that with some CSS instead. 此外,如果根据需要标记字段纯粹是装饰性的,您可能需要考虑使用某些CSS来处理它。 Eg: 例如:

var SponsorStatusChanged = function(){
    var $this = $(this);

    if($this.val() == 'Executed Contract') {
        $this.closest('form').addClass('executed');
    }
}

Then, add this CSS: 然后,添加此CSS:

form.executed label.required-for-executed {
    /* whatever rules are conferred by .required */
}

OR add that selector to the .required block. 或者将该选择器添加到.required块。 Also, you'll have to add the class required-for-executed to each of the appropriate labels in the markup (ie don't do it with javascript). 此外,您还required-for-executed标记中的每个相应标签添加required-for-executed的类(即不要使用javascript执行此操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM