一个按钮触发另一个按钮单击事件

时间:2011-09-23 16:49:14

标签: javascript html

我希望在我的团队建设的表格上有两个提交按钮,一个在首页上,一个在下面。我收到了我的技术团队关于添加它的投诉,因为它需要一些服务器端编码以确保用户不会多次点击它。显然他们只有一个按钮,但要将验证添加到两个将是一个问题。

你能不能只用相同的ID调用按钮,并且表单不会将其视为一个按钮?

我认为另一个选项是新按钮甚至在另一个按钮上点击一下。然后他们仍然只有一个单击即使表单,但我得到了我的两个按钮。我该怎么写呢?

谢谢, ADMA

4 个答案:

答案 0 :(得分:37)

我只熟悉ASP.net和C#按钮,但是使用C#你可以将两个不同的按钮连接到同一个点击事件处理程序。您也可以通过使用辅助按钮触发主按钮单击事件来完成客户端操作。这是一个非常简单的例子:

HTML

<input type="button" id="primaryButton" onclick="ExistingLogic()" />
<input type="button" 
       id="secondaryButton" 
       onclick="document.getElementById('primaryButton').click()" />

答案 1 :(得分:2)

from sklearn.feature_extraction.text import TfidfVectorizer

tfidf_vectorizer = TfidfVectorizer(use_idf=True)
sampleText = []
sampleText.append("Some text for document clustering")
tfidf_matrix = tfidf_vectorizer.fit_transform(sampleText)
X = tfidf_vectorizer.fit_transform(jobDescriptions).todense()

from sklearn.decomposition import PCA
pca = PCA(n_components=2).fit(X)
data2D = pca.transform(X)  

import matplotlib.cm as cm
from sklearn.metrics import silhouette_samples, silhouette_score
import matplotlib.pyplot as plt


for num_clusters in range(2,6):
# Create a subplot with 1 row and 2 columns
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.set_size_inches(18, 7)

# The 1st subplot is the silhouette plot
# The silhouette coefficient can range from -1, 1 but in this example all
# lie within [-0.1, 1]
ax1.set_xlim([-0.1, 1])
# The (n_clusters+1)*10 is for inserting blank space between silhouette
# plots of individual clusters, to demarcate them clearly.
ax1.set_ylim([0, len(data2D) + (num_clusters + 1) * 10])

km = KMeans(n_clusters=num_clusters,
            n_init=10,                        # number of iterations with different seeds
            random_state=1                    # fixes the seed 
           )

cluster_labels = km.fit_predict(data2D)

# The silhouette_score gives the average value for all the samples.
# This gives a perspective into the density and separation of the formed
# clusters
 silhouette_avg = silhouette_score(data2D, cluster_labels)

答案 2 :(得分:0)

如果您想使用vanillaJS进行此操作...这是一条很长的通用方法(使用函数可以清楚地了解正在发生的事情。)

html

<input type="button" id="primaryButton" />
<input type="button" id="secondaryButton"/>

脚本

const primary = document.getElementById('primaryButton');
const secondary = document.getElementById('secondaryButton');

function somePrimaryAction(e){
  e.preventDefault();
  console.log('you clicked the primary button');
}

function clickPrimaryButton(e){
  e.preventDefault();
  console.log('you clicked the secondary button');
  primary.click();
}

primary.addEventListener("click", somePrimaryAction, false);
secondary.addEventListener("click", clickPrimaryButton, false);

答案 3 :(得分:0)

Yeezy

<button onclick="$('#button2').click()">button 1</button>
<button id="button2" onclick="doSomethingWhenClick()">button 2</button>

(((你需要jQuery来运行这个)))