C# 如何查找<;输入类型=文本>;如果不存在,则将其添加到C中#
我通过jQuery动态添加了多个文本框。但由于某些原因,某些文本框的id丢失 我想检查所有的文本框是否有ID。如果没有,则使用C#添加它 我已经从后端完成了这一部分:C# 如何查找<;输入类型=文本>;如果不存在,则将其添加到C中#,c#,C#,我通过jQuery动态添加了多个文本框。但由于某些原因,某些文本框的id丢失 我想检查所有的文本框是否有ID。如果没有,则使用C#添加它 我已经从后端完成了这一部分: // Say I have 3 textboxes added in string "Question" here. string Question="<input data-size="0" data-type="0" id="Answer
// Say I have 3 textboxes added in string "Question" here.
string Question="<input data-size="0" data-type="0" id="Answer1" placeholder="Answer1" type="text" /><br/>
<br/>
<input data-size="0" data-type="0" id="Answer2" placeholder="Answer2" type="text" /><br/>
<br/>
<input data-size="0" data-type="0" id="Answer3" placeholder="Answer3" type="text" /><br/>
<br/>
";
string textboxTagPattern= @"(<input)([^>]*)(type=\"")(text)(\"")([^>]*)(/>)";
Regex rgx = new Regex(textboxTagPattern, RegexOptions.IgnoreCase);
MatchCollection questionInputText = rgx.Matches(Question);
string textboxes ="";
if (questionInputText.Count > 0){
for (int i = 0; i < questionInputText.Count; i++)
{
textboxes = questionInputText[i].Value;
var id = textboxes.IndexOf("id");
if (textboxes.IndexOf("id") == -1)
{
I am stuck here. How to add the id attribute to the textboxes?
}
}
}
//假设我在这里的字符串“Question”中添加了3个文本框。
string Question=“
";
字符串textboxTagPattern=@“(]*)(类型=\”)(文本)(\”)([^>]*)(/>”);
Regex rgx=new Regex(textboxTagPattern,RegexOptions.IgnoreCase);
MatchCollection questionInputText=rgx.Matches(问题);
字符串textboxs=“”;
如果(questionInputText.Count>0){
for(int i=0;i
您可以像下面这样使用。在“id=\“Answer1\”
中使用适当的值,而不是Answer1
if(textboxs.IndexOf(\“id\”)==-1)
{
textboxs=textboxs.Insert(textboxs.IndexOf(“您可以像下面这样使用。使用适当的值,而不是中的Answer1
“id=\“Answer1\”
)
if(textboxs.IndexOf(\“id\”)==-1)
{
textboxs=textboxs.Insert(textboxs.IndexOf)(“一般来说,您不应该使用正则表达式来解析HTML。它是一个。但这里是为了说明。请改用一个。使用JQuery的.Prop()方法。您可以添加您在c#web应用程序中使用的web呈现技术吗?包括随时间的演变。web表单、MVC、Razor、单页应用程序(Angular、React、Vue)。一般来说,您不应该使用正则表达式解析HTML。它是一个。但下面是一个示例。请改用一个。使用JQuery的.Prop()方法。能否添加在c#web应用程序中使用的web渲染技术?包括随时间的演变。web窗体、MVC、Razor、单页应用程序(Angular、React、Vue)。
if (textboxes.IndexOf(\"id\") == -1)
{
textboxes = textboxes.Insert(textboxes.IndexOf("<input ") + "<input ".Length, "id=\"Answer1\" ");
}