简体   繁体   English

如何通过使用同一按钮使用文本框的两个ID来执行两种不同的操作

[英]How to perform two different action by using two ID's of textbox using one same button

In below textbox i have two different id's (ie 's' and 'primary-search-input') and only one button. 在下面的文本框中,我有两个不同的ID(即“ s”和“ primary-search-input”),并且只有一个按钮。

The first id(ie s) is a plugin id for performing search-suggestion work whenever the user enter any keyword he will get related suggestion. 第一个ID是插件ID,用于在用户输入任何关键字会获得相关建议时执行搜索建议工作。 And second id(ie primary-search-input) for getting the search keyword result when the user click on a button. 第二个id(即primary-search-input),用于在用户单击按钮时获取搜索关键字结果。

In the above case both id's has different different tasks. 在上述情况下,两个ID都有不同的不同任务。 when i put id='s' first and id='primary-search-input'second like below it will only perform first id action(ie get suggestion) and when i put id='primary-search-input' first and id='s'as a second position then it will only perform first id action(ie get keyword result on button click). 当我将id =的第一和id ='primary-search-input'第二,如下所示时,它将仅执行第一个id动作(即获得建议),当我将id ='primary-search-input'放在第一和id时=''作为第二个位置,那么它将仅执行第一个id操作(即,在单击按钮时获取关键字结果)。

My query is that how i can perform both the action at the same time like when user type any text into textbox he will get suggestion and also same time he click on button get keyword result on button click. 我的查询是,我如何同时执行两项操作,例如,当用户在文本框中输入任何文本时,他将获得建议,同时单击按钮时,也将在单击按钮时获得关键字结果。

<input name="s"  id="s" id="primary-search-input"  class="wide input" type="search" value="<?php if(isset($_GET['s']) && !isset($_GET['product'])) { echo sanitize_text_field(stripslashes(trim($_GET['s']))); } ?>" placeholder="<?php _e('I am shopping for...', 'framework')?>" />

<div id="primary-search-btn" class="medium primary btn"><a href="#"><i class="icon-search"></i></a></div>

You can't use multiple id in single input. 您不能在单个输入中使用多个ID。 From the XHTML 1.0 Spec 从XHTML 1.0 Spec

In XML, fragment identifiers are of type ID, and there can only be a single attribute of type ID per element. 在XML中,片段标识符的类型为ID,每个元素只能有一个类型为ID的属性。 Therefore, in XHTML 1.0 the id attribute is defined to be of type ID. 因此,在XHTML 1.0中,将id属性定义为ID类型。 In order to ensure that XHTML 1.0 documents are well-structured XML documents, XHTML 1.0 documents MUST use the id attribute when defining fragment identifiers on the elements listed above. 为了确保XHTML 1.0文档是结构良好的XML文档,在上面列出的元素上定义片段标识符时,XHTML 1.0文档必须使用id属性。 See the HTML Compatibility Guidelines for information on ensuring such anchors are backward compatible when serving XHTML documents as media type text/html. 有关确保在将XHTML文档作为媒体类型text / html提供服务时确保此类锚向后兼容的信息,请参见HTML兼容性指南。

You can only have one ID per element, but you can indeed have more than one class. 每个元素只能有一个ID,但实际上可以有多个类。 But don't have multiple class attributes, put multiple class values into one attribute. 但是没有多个类属性,请将多个类值放入一个属性中。

There can only be a single attribute of type ID per element. 每个元素只能有一个类型为ID的属性。 However you can use data-* attributes . 但是,您可以使用data-* attributes The data-* attributes gives us the ability to embed custom data attributes on all HTML elements. data-* attributes使我们能够在所有HTML元素上嵌入自定义数据属性。

You can have the HTML like this: 您可以使用如下所示的HTML:

 <input name="s"  id="s" data-id="primary-search-input"  class="wide input" type="search" value="<?php if(isset($_GET['s']) && !isset($_GET['product'])) { echo sanitize_text_field(stripslashes(trim($_GET['s']))); } ?>"   placeholder="<?php _e('I am shopping for...', 'framework')?>" />

<div id="primary-search-btn" class="medium primary btn"><a href="#"><i class="icon-search"></i></a></div>   

Refer this link for better understanding. 请参阅此链接以获得更好的理解。

You cannot use two id for an input. 您不能将两个id用作输入。 Id must be unique for a webpage. 网页的ID必须是唯一的。 I think you should need to do some toggle programming like that - 我认为您应该像这样进行一些切换编程-

<input type="button" id="s" value="" onclick="toggle()">
<script>

var flag = 0;
function toggle(){

    if(flag == 0){
        // do some work whatever you wanna do first
        flag=1;
    }else{
        // Do some task whatever you wanna do next
        flag = 0;
    }
}
</script>

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

相关问题 使用 flask 执行两个不同的功能和一个提交按钮 - perform two different functions and one submit button using flask 如何使用不同的表 ID 显示两个 html 表? - How to show two html tables using different table id's? 是否可以通过一个按钮提交两个表单,然后两个表单执行两个不同的操作? - Is it possible how to two forms submit by one button and then two forms perform two different actions? 如何使用JavaScript使用一个按钮在两种不同的颜色之间切换 - How to switch between two different colors with one button using javascript 如何在执行操作之前使用 jQuery 检查是否单击了两个按钮 - How to check if two buttons are clicked using jQuery before perform an action 如何用一个按钮交替调用两个ID? - How to call two ID's alternatively with one button? 如何在羽毛应用程序的mongodb中同时使用相同的id删除两个不同集合中的文档 - How to remove document in two different collection using same id at same time in mongodb on feathers application 如何在使用 js 单击 HTML 中的按钮时执行两个操作? - How to perform two action while click a button in HTML with js? 两次单击以使用 requirejs 执行按钮操作? - Two clicks to perform button action with requirejs? 两个不同的Div具有相同的ID和类名,其中一个是Work - Two different Div's have same ID and Class name which one is Work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM