将值标记添加到 html 元素

Add value tag to a html Element

Picture In Response to a Answer What The Webpage looks Like

<input name="ftitle" class="inputbox ui-autocomplete-input" type="text" autocomplete="off">

我需要将 value= 添加到上面的 HTML 输入元素 我该怎么做呢?对于我的生活,我无法弄清楚。

编辑:我需要这个,因为我需要填写网页上不包含 value= 的文本框。但是如果我右键单击它并添加属性 value= 然后我可以通过我的程序更改文本。

我正在使用 C# web 浏览器控件,所以我正在使用

HtmlElement NewAttribute = doc.GetElementById("ftitle");

所以当一切都说完之后,它看起来像这样。

<input name="ftitle" class="inputbox ui-autocomplete-input" type="text" 
 autocomplete="off" value="">

如果它有助于了解我在做什么,这里是我到目前为止所得到的。这都是针对不同的网页,但我正在做同样的事情,但我需要添加 class "value"

 private void Generate_Click(object sender, EventArgs e)
    {




        HtmlDocument doc = wbNewProject.Document;



        HtmlElement wbJobName = doc.GetElementById("Name"); //lblcontact.text
        HtmlElement wbEngineer = doc.GetElementById("engineer-lookup"); //
        HtmlElement wbSalesEng = doc.GetElementById("SalesEngineerUserId");
        HtmlElement wbLocation = doc.GetElementById("Location");
        HtmlElement wbBidDate = doc.GetElementById("BidDate");
        HtmlElement wbPriorApproval = doc.GetElementById("PriorApproval"); //True or False
        HtmlElement wbTakeOff = doc.GetElementById("TakeOffComplete"); //True or False
        HtmlElement wbProject = doc.GetElementById("RoleType"); //Design/Build or Plan/Spec
        HtmlElement element = wbNewProject.Document.GetElementById("ftitle");




        try
        {
            wbJobName.SetAttribute("value", lblJobName.Text);

            if (lblContact.Text.Contains("Dan"))
                wbSalesEng.SetAttribute("value", "2");
            if (lblContact.Text.Contains("Kelley"))
                wbSalesEng.SetAttribute("value", "3");
            if (lblContact.Text.Contains("Erv"))
                wbSalesEng.SetAttribute("value", "4");
            if (lblContact.Text.Contains("Marc"))
                wbSalesEng.SetAttribute("value", "5");
            if (lblContact.Text.Contains("Terry"))
                wbSalesEng.SetAttribute("value", "6");
            if (lblContact.Text.Contains("Chad"))
                wbSalesEng.SetAttribute("value", "7");
            if (lblContact.Text.Contains("Jacob Lenertz"))
                wbSalesEng.SetAttribute("value", "10");
            if (lblContact.Text.Contains("Terry"))
                wbSalesEng.SetAttribute("value", "11");
            if (lblContact.Text.Contains("Nate"))
                wbSalesEng.SetAttribute("value", "12");

            wbLocation.SetAttribute("value", lblLocation.Text);
            wbBidDate.SetAttribute("value", lblBidDate.Text);

            if (lblPriorApp.Text.Contains("Yes"))
                wbPriorApproval.SetAttribute("value", "true");
            if (lblPriorApp.Text.Contains("No"))
                wbPriorApproval.SetAttribute("value", "false");
            if (lblTakeOff.Text.Contains("Done"))
                wbTakeOff.SetAttribute("value", "true");
            if (lblTakeOff.Text.Contains("Not Done"))
                wbTakeOff.SetAttribute("value", "false");

            wbEngineer.SetAttribute("value", lblEngineer.Text);
            wbProject.SetAttribute("value", lblProject.Text);
        }
        catch { }
    }

Javascript

document.getElementById(elementName).value = "your value";

jQuery

$('#elementName').val() = "your value";

这两种方法都需要您在元素上创建一个 Id。但是,jQuery 还允许您通过 other means other than an Id.

select 您的元素

Web 表单 c#

这个也简单,用SetAttribute Method:

HtmlElement element = doc.GetElementById("ftitle"); //Your have this, correct?

element.SetAttribute("value", "your value");

您可以采用这种令人难以置信的骗局方法:

$(document).ready(function() {
     var title = @model.Title;
     $('[name="title"]').val(title);
});

您也可以直接实现 Razor:

<input type="text" value="@model.Title" />

不过请记住,如果@model 为空,您将收到应用程序错误。但正如所指出的,这是一种方法。我诚实地采用的方法是 JavaScript 模板,然后将我的数据模型直接解析到模板,然后将模板直接附加到视图状态。