首页 > 解决方案 > 如何为内联 JQuery 按钮设置 id

问题描述

我正在尝试将 id 分配给对话框上的现有按钮,这些按钮会动态添加到对话框中。

我已经尝试过这些(将 id 分配给 jquery 对话框按钮)但没有用。我在 jsp 页面上使用 Jquery 1.11.1

<div id='dialog_acknowledgement' title='someTitle' style='display: none;'>
    <span style="text-align:left">
        <p><%=popupMessage%></p>
    </span>
</div>

...

$( 'div#dialog_acknowledgement').dialog( { 
    modal: true, 
    autoOpen: true,
    width: 750,
    position: [ 'center', 'center' ],
    open: function() { jQuery( '.ui-dialog-titlebar-close' ).hide(); },
    close: function() { 
            if ($( 'input[name=acknowledgementAction]' ).val() == "") {
                $( 'input[name=acknowledgementAction]' ).val( "CANCEL" );
            }
            $( "form[name='page_form']" ).submit();
        },
    buttons: {
        "<%=lh.getText("review_jsp_23", LocalizableTag.CONFIG_MESSAGE, "Cancel")%>": function() {   
            $( 'input[name=acknowledgementAction]' ).val( "CANCEL" ); // trying to set the id for these buttons
            $( this ).dialog( "close" );
        },
        "<%=lh.getText("review_jsp_27", LocalizableTag.CONFIG_MESSAGE, "ACKNOWLEDGE")%>": function() {
            $( 'input[name=acknowledgementAction]' ).val( "ACKNOWLEDGE" );
            $( this ).dialog( "close" );
        }
    }
} );

这是我尝试设置 id 的选项之一时生成的 js

buttons: {
    "Cancel": function() {  
        $(this).attr("id","cancelButton_1");                                
        $( 'input[name=acknowledgementAction]' ).val( "CANCEL" );
        $( this ).dialog( "close" );
        //window.location.href = '../../etm/etmMenu.jsp?selectedTocID=null&parentID=0'
    },
    "ACKNOWLEDGE": function() {
        $( 'input[name=acknowledgementAction]' ).val( "ACKNOWLEDGE" );
        $( this ).dialog( "close" );
    }
}

这是生成的html,看不到id

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
    <div class="ui-dialog-buttonset">
        <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
            <span class="ui-button-text">Cancel</span>
        </button>
        <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
            <span class="ui-button-text">ACKNOWLEDGE</span>
        </button>
    </div>
</div>

我想为这些按钮添加 id,以便我可以将它们用于我的 selenium 测试用例

标签: jquery

解决方案


推荐阅读